//
//  HYUserLoginVC.m
//  TJHYHardwareProduct
//
//  Created by yc on 2017/12/25.
//  Copyright © 2017年 Cong Yao. All rights reserved.
//

#import "HYUserLoginVC.h"
#import "PhoneTextFieldView.h"
#import "PasswordTextFieldView.h"
#import "LoginView.h"
#import "HYLoginApi.h"
#import "PublicGetUserInfoApi.h"

@interface HYUserLoginVC ()

@property(nonatomic,strong)LoginView *containerView;

@end

@implementation HYUserLoginVC

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

#pragma mark --- 点击事件
- (void)didClickLoginButton
{
    if (self.containerView.phoneView.phoneTextField.text.length == 0) {
        
        [SVProgressHUD showErrorWithStatus:@"请输入正确的用户名"];
        return;
    }
    
    if (![self.containerView.passwordView.passwordTextField.text isRegexPassword]) {
        [SVProgressHUD showErrorWithStatus:@"请输入符合规定的密码"];
        return;
    }
    
    [self loginActionStart];

}

- (void)loginActionStart
{
    [SVProgressHUD showInfoWithStatus:@"正在登录中..."];
    NSString *username = self.containerView.phoneView.phoneTextField.text;
    NSString *password = self.containerView.passwordView.passwordTextField.text;
    [HYLoginApi login:username password:password success:^(NSDictionary *dic) {
        [SVProgressHUD showSuccessWithStatus:@"登录成功"];
        SAVE_OBJECT(@"YES", LOGIN);
        SAVE_BOOL(self.containerView.phoneView.phoneTextField.text, USERNAME);
        SAVE_OBJECT(dic[@"data"][@"uid"], UID);
        SAVE_OBJECT(dic[@"data"][@"token"], TOKEN);
        [self GetUserInfo];
    } fail:^(NSDictionary *dic) {
        if (dic == nil)
        {
            [SVProgressHUD showErrorWithStatus:@"网络异常"];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:dic[@"errmsg"]];
        }
    }];
}

- (void)GetUserInfo {
    
    PublicGetUserInfoApi * userInfoApi = [[PublicGetUserInfoApi alloc] init];
    [userInfoApi startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        [userInfoApi analyseResponseObject];
        SAVE_OBJECT(@"YES", LOGIN);
        [[AppDelegate defaultService]setRootViewController];
    } failure:^(YTKBaseRequest *request) {
        [SVProgressHUD showErrorWithStatus:@"获取用户信息失败！"];
    }];
}

#pragma mark --- 懒加载
- (LoginView *)containerView
{
    if(!_containerView)
    {
        UIView *superView = self.view;
        _containerView = [[LoginView alloc]init];
        [superView addSubview:_containerView];
        _containerView.phoneView.phoneTextField.text = EXRACT_OBJECT(USERNAME);
        [_containerView.loginButton addTarget:self action:@selector(didClickLoginButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _containerView;
}

#pragma 初始化视图
- (void)base_masLayoutSubview
{
    __weak UIView *superView = self.view;
    
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(superView);
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
